package com.andy.wang.service.impl;

import com.andy.wang.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.andy.wang.mapper.UserMapper;
import com.andy.wang.service.SmsService;
import com.andy.wang.utils.SmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    UserMapper userMapper;
    //短信发送
    @Override
    public String send(String mobile, Map<String, Object> param) {
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<User>();
        query.eq(User::getUPhone,mobile);
        Long i =userMapper.selectCount(query);
        if (i <= 0) {
            return "404";
        } else {
            CCPRestSmsSDK sdk = new CCPRestSmsSDK();
            sdk.init(SmsProperties.SERVER_IP, SmsProperties.SERVER_PORT);
            sdk.setAccount(SmsProperties.ACCOUNT_SID, SmsProperties.ACCOUNT_TOKEN);
            sdk.setAppId(SmsProperties.APP_ID);
            sdk.setBodyType(BodyType.Type_JSON);
            String[] datas = {(String) param.get("code"), "5"};
            HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, "1", datas);
            System.out.println("SMS--Result: " + result);
            if ("000000".equals(result.get("statusCode"))) {
                HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
                Set<String> keySet = data.keySet();
                for (String key : keySet) {
                    Object object = data.get(key);
                    System.out.println(key + " = " + object);
                }
                return (String) param.get("code");
            } else {
                System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
                return "404";
            }
        }
    }
}
